

package com.soso.base.google.comm;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.quota.QuotaService;
import com.google.appengine.api.quota.QuotaServiceFactory;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.soso.base.google.cache.CommonCache;
import com.soso.base.google.cache.CommonCacheImpl;
import com.soso.base.google.comm.impl.CacheServiceImpl;
import com.soso.base.google.mq.MessageQueue;

/**
 * 
 * @author speed
 *
 */
public abstract class GAEContext {
	private int requestCount = 0;
	private long startTime = System.currentTimeMillis();
	private List<String> skipURLs;
	private Locale locale;
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private MessageQueue messageQueue;
	private BaseService baseService;
	
	private CacheService cache;
	private CommonCache commonCache;
	

	public boolean isCPUTimeout(final int TASK_TTL){
		if (getRequestCPUTimeSeconds() > TASK_TTL) {
			return true;
		}
		return false;
	}
	
	public CommonCache getCommonCache() {
		return commonCache;
	}

	public void setCommonCache(CommonCache commonCache) {
		this.commonCache = commonCache;
	}
	public CacheService getCache() {
		return cache;
	}

	public void setCache(CacheService cache) {
		this.cache = cache;
	}
	
	public long getRequestCPUTimeSeconds() {
		long result = (System.currentTimeMillis() - getStartTime())/1000;
		return result;
	}
	
	public QuotaService getQuotaService() {
		return QuotaServiceFactory.getQuotaService();
	}

	
	public Queue getQueue(String name) {
		return QueueFactory.getQueue(name);
	}

	public BaseService getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
		requestCount++;
		startTime = System.currentTimeMillis();
	}
	
	public String getLanguage() {
		return locale.getLanguage();
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale aLocale) {
		locale = aLocale;
	}
	
	public int getRequestCount() {
		return requestCount;
	}

	public long getStartTime() {
		return startTime;
	}

	public List<String> getSkipURLs() {
		return skipURLs;
	}

	public void setSkipURLs(List<String> skipURLs) {
		this.skipURLs = skipURLs;
	}
	
	public boolean isSkipUrl(final String url) {
    	for (String u : skipURLs) {
    		if (url.startsWith(u)) {
    			return true;
    		}
    	}
    	return false;
    }

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public MessageQueue getMessageQueue() {
		return messageQueue;
	}

	public void setMessageQueue(MessageQueue messageQueue) {
		this.messageQueue = messageQueue;
	}
	
	public void init(){
    	if(this.getCache()==null){
    		this.setCache(new CacheServiceImpl());
    	}
    	if(this.getCommonCache()==null){
    		this.setCommonCache(new CommonCacheImpl(this.getCache()));
    	}
    	
	}

}
